<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="悬赏编号" prop="offerCode">
        <el-input
          v-model="queryParams.offerCode"
          placeholder="请输入悬赏编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="被悬赏人" prop="offerName">
        <el-input
          v-model="queryParams.offerName"
          placeholder="请输入被悬赏人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="执行进度" prop="schedule">
        <el-select
          v-model="queryParams.schedule"
          placeholder="执行进度"
          clearable
        >
          <el-option
            v-for="dict in dict.type.bus_schedule"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="初审" prop="checked">
        <el-select
              v-model="queryParams.checked"
              placeholder="初审状态"
              clearable
              style="width: 140px"
            >
              <el-option
                v-for="dict in dict.type.bus_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
      </el-form-item>
      <el-form-item label="复审" prop="repeatChecked">
        <el-select
              v-model="queryParams.repeatChecked"
              placeholder="复审状态"
              clearable
              style="width: 140px"
            >
              <el-option
                v-for="dict in dict.type.bus_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
              v-model="queryParams.status"
              placeholder="状态"
              clearable
              style="width: 140px"
            >
              <el-option
                v-for="dict in dict.type.sys_common_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleEdit()"
          v-hasPermi="['business:offer:add']"
        >新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:offer:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:offer:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="offerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="悬赏编号" align="center" prop="offerCode" />
      <el-table-column label="被悬赏人类型" align="center" prop="offerModel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_offer_model" :value="scope.row.offerModel"/>
        </template>
      </el-table-column>
      <el-table-column label="被悬赏人名称" align="center" prop="offerName" />
      <el-table-column label="证件号码" align="center" prop="offerIdcard" />
      <el-table-column label="案件执行地区" align="center" prop="city" />
      <el-table-column label="执行金额(元)" align="center" prop="amount" width="120" />
      <el-table-column label="悬赏比例(%)" align="center" prop="scale" width="120" />
      <el-table-column label="执行进度" align="center" prop="schedule">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_schedule" :value="scope.row.schedule"/>
        </template>
      </el-table-column>
      <el-table-column label="初审" align="center" prop="checked">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_status" :value="scope.row.checked"/>
        </template>
      </el-table-column>
      <el-table-column label="复审" align="center" prop="repeatChecked">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_status" :value="scope.row.repeatChecked"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_common_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center" prop="createTime" width="120" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleEdit(scope.row.offerId)"
            v-hasPermi="['business:offer:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:offer:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <edit-dialog ref="edit" @result="getList" />
  </div>
</template>

<script>
import { listUser } from "@/api/system/user";
import { listOffer, delOffer, updateOffer } from "@/api/business/offer";
import { VueCropper } from "vue-cropper";
import editDialog from './edit';
export default {
  name: "Offer",
  dicts: ['bus_court','bus_schedule', 'bus_progress', 'bus_status', 'bus_offer_model', 'sys_common_status'],
  components: { editDialog, VueCropper },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 悬赏执行表格数据
      offerList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: 'updateTime desc,createTime desc',
        isAsc: '',
        offerCode: null,
        offerName: null,
        offerIdcard: null,
        mobile: null,
        city: null,
        amount: null,
        scale: null,
        court: null,
        schedule: null,
        description: null,
        file1: null,
        file2: null,
        file3: null,
        file4: null,
        file5: null
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询悬赏执行列表 */
    getList() {
      this.loading = true;
      listOffer(this.queryParams).then(response => {
        this.offerList = response.rows;
        this.offerList.map(item => item.city = item.city !== null ? item.city.split('-')[2] : null)
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.offerId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加悬赏执行";
    },
    handleEdit(offerId) {
      this.$refs.edit.init(offerId);
    },
    /** 查询用户列表 */
    getUserList() {
      listUser().then(response => {
          this.userList = response.rows.filter((_item => {
            if (_item.posts.length > 0 && _item.posts.map(v => v.postId).indexOf(6) != -1) {
              return _item;
            }
            return;
          }));
        }
      );
    },
    /** 初审 */
    handleChecked(row) {
      const str = row[1] == 1 ? '是否确认审核通过悬赏执行编号为"' + row[0].offerId + '"的数据项？' : '是否确认驳回悬赏执行编号为"' + row[0].offerId + '"的数据项？'
      if (row[0].offerId != null) {
          row[0].checked = row[1];
          this.$modal.confirm(str).then(function() {
          return updateOffer(row[0]);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        }).catch(() => {});
      }
    },
    /** 复审 */
    handleRepeatChecked(row) {
      const str = row[1] == 1 ? '是否确认审核通过悬赏执行编号为"' + row[0].offerId + '"的数据项？' : '是否确认驳回悬赏执行编号为"' + row[0].offerId + '"的数据项？'
      if (row[0].offerId != null) {
          row[0].repeatChecked = row[1];
          this.$modal.confirm(str).then(function() {
          return updateOffer(row[0]);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        }).catch(() => {});
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const offerIds = row.offerId || this.ids;
      this.$modal.confirm('是否确认删除悬赏执行编号为"' + offerIds + '"的数据项？').then(function() {
        return delOffer(offerIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/offer/export', {
        ...this.queryParams
      }, `offer_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
